export interface UseLogOptions {
  api: (data: any) => Promise<any>
  body: Record<string, any>
  immediate?: boolean
}

export function useTerminal({ api, immediate, body }: UseLogOptions) {
  const el = ref<HTMLElement>()
  const classNames = computed(() => ['web-terminal__modal'])
  const isEnd = ref(false) // 为true时则表示申请证书成功
  const loading = ref(false)
  const txtContent = ref('')
  const _fetch = (data?: any) => {
    if (!api) {
      console.error('参数 api 必选')
      return
    }
    // reset()
    loading.value = true
    api({ id: body?.id || data?.id })
      .then((res) => {
        isEnd.value = res.status === 1 || res.status === 2
        txtContent.value = res.log
      })
      .finally(() => (loading.value = false))
  }
  const run = (data?: any) => {
    _fetch(data)
  }
  const request = (data: any) => {
    _fetch(data)
  }
  const reset = () => {
    isEnd.value = false
    // txtContent.value = ''
  }
  nextTick(() => {
    if (immediate)
      run()
  })

  return {
    el,
    run,
    request,
    loading,
    classNames,
    reset,
    isEnd,
    txtContent,
  }
}
